<%@ page contentType="text/html; charset=GBK" %><%@ page import="java.util.*" %><%@ page import="java.util.zip.*"%><%@ page import="java.io.*" %><%@ page import="com.ionglobal.db.*" %><%@ page import="com.ionglobal.lib.*" %><%@ page import="com.ionglobal.misc.*" %><%@ page import="com.dhec.ggs.*"%><%
if(request.getRemoteAddr().indexOf("172.16.4")!=-1){
	DBI dbi = new DBI();
	Request Request = new Request(request);
	try{
		String pn = Request.getString("pn").toUpperCase();
		String addr = "";
		String partno = "";
		String dcno = "";
		//根据零件号查找CATIA文件，按版本倒序排，如果有V5(3D)的，会排在前边，优先看3D
		dbi.execQuery("select sFile,sPartNo,sDCNO from ggs_CatiaData where spartno='"+pn+"' order by sversion desc,sFile asc");
		if(dbi.rsNext()){
			addr = dbi.rsGetString("sFile");
			partno = dbi.rsGetString("sPartNo");
			dcno = dbi.rsGetString("sDCNO");
		}
		dbi.rsClose();
		
		//如果是CATProduct，需要下载打包好的多个关联文件
		if(addr.endsWith("CATProduct")){
			File zipFile = new File(addr.substring(0, addr.length()-10)+"zip");
			//如果zip文件不存在，则创建
			if(!zipFile.exists()){
				//构建CRC32校检的zip包输出流
				CheckedOutputStream cos = new CheckedOutputStream(new FileOutputStream(zipFile), new CRC32());  
				ZipOutputStream zos = new ZipOutputStream(cos);
	
				//从数据库中按零件号及设变号找出所有相关的V5(3D)的文件，准备打包
				dbi.execQuery("select sFile from ggs_CatiaData where sPartNo='"+partno+"' and sDcno='"+dcno+"'");
				ArrayList<File> files = new ArrayList<File>();
				while(dbi.rsNext()){
					files.add(new File(dbi.rsGetString("sFile")));
				}
				dbi.rsClose();
				
				//压缩每个文件
				for(int i=0;i<files.size();i++){
					ZipEntry entry = new ZipEntry(files.get(i).getName());
					zos.putNextEntry(entry);
					
					BufferedInputStream bis = new BufferedInputStream(new FileInputStream(files.get(i)));  
					int count;
					int BUFFER = 1024;
					byte data[] = new byte[BUFFER];  
					while ((count = bis.read(data, 0, BUFFER)) != -1) {  
						zos.write(data, 0, count);  
					}  
					bis.close();
					
					zos.closeEntry();
				}
				
				//输出zip文件
				zos.flush();  
				zos.close();
			}
			//返回zip文件路径
			addr = "ftp://172.16.4.1/"+addr.substring(29, addr.length()-10)+"zip";
		}else
			addr = "ftp://172.16.4.1/"+addr.substring(29);
		addr = StringUtil.replaceStr(addr,"\\","/");
		out.print(addr);
	}catch(Exception e){
		e.printStackTrace();
	}finally{
		dbi.closeAll();	
	}
}
%>